﻿using HCLSMod;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

/// <summary>
/// 命名管道命令处理系统
/// 用于命名管道收到数据后，将数据解析成为命令，并执行相关命令，并返回结果
/// </summary>
namespace LNamePipe
{
    [Serializable]
    public class NamePipeCommandBase
    {
        #region (*成员属性*)
        protected string fUserToken = "";
        public string UserToken { get { return fUserToken; } }
        /// <summary>
        /// 命令令牌，创建时自动生成，唯一GUID
        /// </summary>
        public string CommandToken { get; set; }
        /// <summary>
        /// 主序列号
        /// </summary>
        public int IndexMain { get; set; }
        /// <summary>
        /// 子序列号
        /// </summary>
        public int IndexSub { get; set; }
        /// <summary>
        /// 命令包为上一命令的应答，由收到方设置为true，并发回
        /// </summary>
        public bool IsRespon = false;
        /// <summary>
        /// 命令创建时间，创建时自动生成
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 命令携带的数据
        /// </summary>
        protected List<byte> fCommandData = new List<byte>();
        /// <summary>
        /// 命令携带的数据
        /// </summary>
        public byte[] CommandData
        {
            get { return fCommandData.ToArray(); }
            set
            {
                if (value != null)
                    fCommandData = new List<byte>(value);
                else
                    fCommandData = new List<byte>();
            }
        }
        /// <summary>
        /// 命令携带的数据数量
        /// </summary>
        public int DataSize { get { return fCommandData.Count; } }
        #endregion

        #region (*构建函数*)
        public NamePipeCommandBase(Guid fUserTokenX)
        {
            fUserToken = fUserTokenX.ToString().ToUpper();
            CommandToken = Guid.NewGuid().ToString().ToUpper();
            CreateTime = DateTime.Now;
        }
        /// <summary>
        /// 创建命令，携带指定的数据
        /// </summary>
        /// <param name="fSendData"></param>
        public NamePipeCommandBase(Guid fUserTokenX,byte[] fSendData) : this(fUserTokenX)
        {
            fCommandData = new List<byte>(fSendData);
        }

        public NamePipeCommandBase(Guid fUserTokenX, object fSendData) : this(fUserTokenX)
        {
            PutDatas(fSendData);
        }
        #endregion

        #region (*命令携带的数据控制*)
        /// <summary>
        /// 将对象放入大命令中
        /// </summary>
        /// <param name="fDestData"></param>
        /// <returns></returns>
        public int PutDatas(object fDestData)
        {
            int fResult = 0;
            try
            {
                byte[] fDTemp = BaseLib.SerializeObject(fDestData);
                fResult = fDTemp.Length;
                fCommandData = new List<byte>(fDTemp);
            }
            catch
            {
                fResult = -1;
            }
            return fResult;
        }
        /// <summary>
        /// 将命令中携带数据还原成object
        /// </summary>
        /// <returns></returns>
        public object GetDatas()
        {
            if (DataSize > 0)
                return BaseLib.DeserializeObject(fCommandData.ToArray());
            else
                return null;
        }
        #endregion

        public override string ToString()
        {
            return string.Format("{0}-{1},UserToken={2},CommandToken={3},{4},Time={5},[Data={6}]",
                IndexMain.ToString(),
                IndexSub.ToString(),
                fUserToken,
                CommandToken ?? "",
                IsRespon ? "IsRespon" : "Command",
                CreateTime.ToString("HH:mm:ss FF"),
                fCommandData.Count.ToString());
        }
    }
}
